package Work2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Pro3 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		String[] s = new String[n];
		for (int i = 0; i < s.length; i++) {
			s[i] = in.next();
		}
		while(true){
			String choose = in.next();
			if(choose.equals("sort1"))sort1(s);
			else if(choose.equals("sort2"))sort2(s);
			else if(choose.equals("e")){System.out.println("exit");break;}
		}
	}

	private static void sort1(String[] str) {
		//String s[] = new String[str.length];
		List<String> s = new ArrayList();
		for (int j = 0; j < str.length; j++) {
		s.add(str[j].substring(6, 10) + "-" + str[j].substring(10, 12)
				+ "-" + str[j].substring(12, 14));
		}
		Collections.sort(s);
		for(String temp:s){
			System.out.println(temp);
		}
	}

	private static void sort2(String[] str) {
		String s[] = new String[str.length];
		for (int i = 0; i < s.length; i++) {
			s[i] = str[i].substring(6,14);
		}
		slectSort(s,str);
	}

	private static void slectSort(String[] s, String[] str) {
		int minIndex=0;
	    int temp=0;
	    String s1;
	    if((s==null)||(s.length==0))
	        return;
	    int a[] = new int [s.length];
	    for (int i = 0; i < a.length; i++) {
	    	 a[i] = Integer.valueOf(s[i]);
		}
	    
	    for(int i=0;i < a.length-1;i++)
	    {
	        minIndex=i;
	        for(int j=i+1;j<s.length;j++)
	        {
	            if(a[j] < a[minIndex])
	            {
	                minIndex=j;
	            }
	        }
	        if(minIndex!=i)
	        {
	            temp=a[i];
	            s1 = str[i];
	            a[i]=a[minIndex];
	            str[i] = str[minIndex];
	            a[minIndex]=temp;
	            str[minIndex] = s1;
	        }
	    }
		for (int i = 0; i < str.length; i++) {
			System.out.println(str[i]);
		}
	}
    
}   